﻿//
//This file contains the functions for zooming in and out in the gallery
//

//zooms out
function zoomOutStep1()
{
    if(isZooming)
        return;
        
    isZooming = true;
    
    host.findName("btnPrevious").Opacity = 0;
    host.findName("btnNext").Opacity = 0;

    //zoom out
    var scaleX = host.findName("zoomScaleX");
    var scaleY = host.findName("zoomScaleY");
    var translateX = host.findName("zoomTranslateX");
    var translateY = host.findName("zoomTranslateY");
    var zoomAnimation = host.findName("zoomAnimation");

    scaleX.To = 1;
    scaleY.To = 1;
    scaleX.Duration = "0:0:" + zoomDuration;
    scaleY.Duration = "0:0:" + zoomDuration;
    translateX.To = 0;
    translateY.To = 0;
    
    zoomAnimation.Begin();
    zoomedIn = false;
    
    window.setTimeout("zoomOutStep2()", zoomDuration * 1000);
}

//re-arranges the images in a grid and rotates them at a random angle
function zoomOutStep2()
{
    //build grid
    for(i = 0; i < photoCanvas.children.Count; i++)
    {
        var img = photoCanvas.children.getItem(i);
        var translationStoryBoard = img.findName("imgTranslationStoryboard" + i);
        var translationAnimationX = img.findName("imgTranslateAnimationX" + i);
        var translationAnimationY = img.findName("imgTranslateAnimationY" + i);
        var squareAmount = Math.floor(Math.sqrt(photoCanvas.children.Count));
        
        translationAnimationX.From = img["Canvas.Left"];
        translationAnimationY.From = img["Canvas.Top"];
        
        translationAnimationX.To = imageSpacing + ((i%squareAmount) * (img.Width + imageSpacing));
        translationAnimationY.To = imageSpacing + Math.floor(i/squareAmount) * img.Height;
        
        translationAnimationX.Duration = "0:0:" + shapeChangeDuration;
        translationAnimationY.Duration = "0:0:" + shapeChangeDuration;
        
        var imgAnim = img.FindName("imgRotationAnimation" + i);
        imgAnim.To = (Math.random() - 0.5) * 15;
        imgAnim.Duration = "0:0:" + shapeChangeDuration;
        img.FindName("imgRotationStoryboard" + i).Begin();
        
        translationStoryBoard.Begin();
    }
    
    isZooming = false;
}

//arranges the images in a line and removes the random rotation
function zoomInStep1()
{
    if(isZooming)
        return;
        
    isZooming = true;

    //build line
    for(i = 0; i < photoCanvas.children.Count; i++)
    {
        var img = photoCanvas.children.getItem(i);
        var translationStoryBoard = img.findName("imgTranslationStoryboard" + i);
        var translationAnimationX = img.findName("imgTranslateAnimationX" + i);
        var translationAnimationY = img.findName("imgTranslateAnimationY" + i);
        var squareAmount = Math.floor(Math.sqrt(photoCanvas.children.Count));
        
        translationAnimationX.From = img["Canvas.Left"];
        translationAnimationY.From = img["Canvas.Top"];
        
        translationAnimationX.To = imageSpacing + i * (img.Width + imageSpacing);
        translationAnimationY.To = imageSpacing;
        
        translationAnimationX.Duration = "0:0:" + shapeChangeDuration;
        translationAnimationY.Duration = "0:0:" + shapeChangeDuration;

        var imgAnim = img.FindName("imgRotationAnimation" + i);
        imgAnim.To = 0;
        imgAnim.Duration = "0:0:" + shapeChangeDuration;
        img.FindName("imgRotationStoryboard" + i).Begin();
    
        translationStoryBoard.Begin();
    }
    
    window.setTimeout("zoomInStep2()", shapeChangeDuration * 1000)
}

//zooms the clicked image to the maximum possible size (regarding the available space)
function zoomInStep2()
{
    //zoom in
    var scaleX = host.findName("zoomScaleX");
    var scaleY = host.findName("zoomScaleY");
    var translateX = host.findName("zoomTranslateX");
    var translateY = host.findName("zoomTranslateY");
    var scaleTransform = host.findName("zoomScale");
    var zoomAnimation = host.findName("zoomAnimation");
    var zoomFactor = Math.min(host.ActualWidth, host.ActualHeight) / maxImageSize;
    /([0-9]+)/.exec(clickedImage.Name)
    currentImageNr = parseInt(RegExp.$1);
    
    scaleX.To = zoomFactor;
    scaleY.To = zoomFactor;
    translateX.To = -1 * (currentImageNr * (clickedImage.Width + imageSpacing) + imageSpacing) * zoomFactor - (((zoomFactor * clickedImage.Width) - host.ActualWidth) / 2);
    translateY.To =  -1 * (imageSpacing * zoomFactor) - (((zoomFactor * clickedImage.Height) - host.ActualHeight) / 2);
    
    scaleX.Duration = "0:0:" + zoomDuration;
    scaleY.Duration = "0:0:" + zoomDuration;
    translateX.Duration = "0:0:" + zoomDuration;
    translateY.Duration = "0:0:" + zoomDuration;
    
    scaleTransform.CenterX = 0;
    scaleTransform.CenterY = 0;
    
    zoomAnimation.Begin();
    
    host.findName("btnPrevious").Opacity = 1;
    host.findName("btnNext").Opacity = 1;
    
    zoomedIn = true;
    isZooming = false;
    
    checkPosition();
}